package com.jhws.util;

import org.apache.commons.lang.time.DateFormatUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {
    // 短日期格式
    public static String DATE_FORMAT = "yyyy-MM-dd";

    // 长日期格式
    public static String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    /**
     * @Comment: 获取现在时间，到秒
     * @Param:
     */
    public static String getTimeNowFmtSec() {
        return getTimeNowFmtSec("yyyy-MM-dd HH:mm:ss");
    }

    /**
     * @Comment: 获取现在时间，到秒
     * @Param:
     */
    public static String getTimeNowFmtSec(String pattern) {
        DateFormat format = new SimpleDateFormat(pattern);
        return format.format(new Date());
    }

    public static long getTimeStampsNowFmtSec() {
        long time = System.currentTimeMillis();
        return time / 1000;
    }

    /**
     * 获取当前时间Date类型
     * Created on 2014-6-6
     *
     * @return Date
     */
    public static Date parseDateFormat() {
        return parseDateFormat("yyyy-MM-dd");
    }

    public static Date parseDateFormat(String format) {
        SimpleDateFormat fo = new SimpleDateFormat();
        Date date = new java.util.Date(System.currentTimeMillis());
        fo.applyPattern(format);
        try {
            date = fo.parse(DateFormatUtils.format(date, format));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 格式化时间
     *
     * @param date
     * @return
     */
    public static String parseDateFormat(Date date) {
        return parseDateFormat(date, TIME_FORMAT);
    }

    /**
     * 格式化时间
     *
     * @param date
     * @return
     */
    public static String parseDateFormat(Date date, String pattern) {
        DateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }

    /**
     * @Comment: 获取今天日期，格式到日为止
     * @Param:
     */
    public static String getTimeNowFmtDay() {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(new Date());
    }

    /**
     * @Comment: 获取昨天的日期, 格式到日为止
     * @Param:
     */
    public static String getTimeYestodayFmtDay() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
    }

    // 获取前后日期 i为正数 向后推迟i天，负数时向前提前i天
    public static Date getDateByDay(Date date, int i) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, i);
        return cal.getTime();
    }

    // 获取前后日期 i为正数 向后推迟i天，负数时向前提前i天
    public static String getDateByDay(Date date, int i, String pattern) {
        return new SimpleDateFormat(pattern).format(getDateByDay(date, i));
    }

    /**
     * @Comment: 获取格式化过的某天的前一天
     * @Param: dateStr - yyyy-MM-dd 格式的日期字符串
     */
    public static String getTheDayBefore(String dateStr) {
        try {
            Date date = new SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            int day = cal.get(Calendar.DATE);
            cal.set(Calendar.DATE, day - 1);
            return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @Comment: 获取格式化过的一周的时间
     * @Param: aweek前几周
     */
    public static List<String> getTheAweek(int aweek) {
        List<String> dates = new ArrayList<String>();
        int j = (aweek - 1) * 7;
        for (int i = j; i < j + 7; i++) {
            String date = getdate(-i);
            dates.add(date);
        }
        return dates;
    }

    /**
     * 获取指定月数之后的日期
     *
     * @param month
     * @return
     */
    public static Date getDateByMonth(Date date, int month) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, month);
        return cal.getTime();
    }

    /**
     * 获取指定月数之后的格式化日期
     *
     * @param month
     * @param pattern
     * @return
     */
    public static String getDateByMonth(Date date, int month, String pattern) {
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        return df.format(getDateByMonth(date, month));
    }

    /**
     * 获取当前日期指定月数之后的格式化日期
     *
     * @param month
     * @param pattern
     * @return
     */
    public static String getDateByMonth(int month, String pattern) {
        return getDateByMonth(new Date(), month, pattern);
    }

    /**
     * 获取指定月数之前的日期
     *
     * @param month
     * @return
     */
    public static Date getDateByFrontMonth(Date date, int month) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, -month);
        return cal.getTime();
    }

    /**
     * 获取指定月数之前的格式化日期
     *
     * @param month
     * @param pattern
     * @return
     */
    public static String getDateByFrontMonth(Date date, int month, String pattern) {
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        return df.format(getDateByFrontMonth(date, month));
    }

    /**
     * 获取当前日期指定月数之前的格式化日期
     *
     * @param month
     * @param pattern
     * @return
     */
    public static String getDateByFrontMonth(int month, String pattern) {
        return getDateByFrontMonth(new Date(), month, pattern);
    }

    public static void main(String args[]) {
        System.out.println("Hello World!---》    "+getDateByFrontMonth(5, TIME_FORMAT));
    }

    /**
     * 将日期格式的字符串转换为长整型(毫秒)
     *
     * @param date
     * @param format
     * @return
     */
    public static long convert2long(String date, String format) {
        try {
            if (StringUtils.isNotBlank(date)) {
                if (StringUtils.isBlank(format)) {
                    format = TIME_FORMAT;
                }
                SimpleDateFormat sf = new SimpleDateFormat(format);
                return sf.parse(date).getTime();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0L;
    }

    /**
     * 将日期格式的字符串转换为长整型(秒)
     *
     * @param date
     * @return
     */
    public static long convert2LongSec(String date) {
        return convert2long(date, TIME_FORMAT) / 1000;
    }


    /**
     * 将给定长整型timeStamp转换为指定格式字符串
     *
     * @param format
     * @return
     */
    public static String timestamp2String(long timeStamp, String format) {
        if (Long.toString(timeStamp).length() < 12)
            timeStamp *= 1000;
        Date date = new Date(timeStamp);
        if (StringUtils.isBlank(format)) {
            format = TIME_FORMAT;
        }
        SimpleDateFormat sf = new SimpleDateFormat(format);
        return sf.format(date);

    }

    /**
     * 时间戳转字符串，秒
     *
     * @param timeStamp
     * @return
     */
    public static String timestamp2StringFmtSec(long timeStamp) {
        return timestamp2String(timeStamp, "yyyy-MM-dd HH:mm:ss");
    }

    // 获取前后日期 i为正数 向后推迟i天，负数时向前提前i天
    public static String getdate(int i) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, i);
        String dformat = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
        return dformat;
    }

    public static Date getdateReturnDate(int i) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, i);
        return cal.getTime();
    }

    /**
     * @param dateString 字符串转date
     * @return
     */
    public static Date stringDate(String dateString) {
        Date date = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            date = sdf.parse(dateString);
        } catch (ParseException e) {
            System.out.println(e.getMessage());
        }
        return date;
    }

    /**
     * @param dateString 字符串转date
     * @return
     */
    public static Date stringDate(String dateString, String pattern) {
        Date date = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            date = sdf.parse(dateString);
        } catch (ParseException e) {
            System.out.println(e.getMessage());
        }
        return date;
    }

    /**
     * 毫秒时间
     * Long类型时间转换成时长
     */
    public static String format(Long time) {
        if (time == null) {
            return null;
        } else {
            Date date = new Date(time);
            long hour = time / (60 * 60 * 1000);
            long minute = (time - hour * 60 * 60 * 1000) / (60 * 1000);
            long second = (time - hour * 60 * 60 * 1000 - minute * 60 * 1000) / 1000;
            return (hour == 0 ? "00" : (hour > 10 ? hour : ("0" + hour))) + ":" + (minute == 0 ? "00" : (minute > 10 ? minute : ("0" + minute))) + ":" + (second == 0 ? "00" : (second > 10 ? second : ("0" + second)));
        }
    }

    /**
     * 时间为秒
     * Long类型时间转换成时长
     */
    public static String formatTime(Long time) {
        if (time == null) {
            return null;
        } else {
            Date date = new Date(time);
            long hour = time / (60 * 60);
            long minute = (time - hour * 60 * 60) / 60;
            long second = time - hour * 60 * 60 - minute * 60;
            StringBuilder stringBuilder = new StringBuilder();
            if (hour > 24) {
                stringBuilder.append(hour / 24).append("天 ").append(hour % 24).append("小时 ").append(minute).append("分 ").append(second).append("秒");
                return stringBuilder.toString();
            } else {
                return (hour == 0 ? "00" : (hour > 10 ? hour : ("0" + hour))) + ":" + (minute == 0 ? "00" : (minute > 10 ? minute : ("0" + minute))) + ":" + (second == 0 ? "00" : (second > 10 ? second : ("0" + second)));
            }
        }
    }

    /**
     * field获取不同时间
     *
     * @param field
     * @return
     */
    public static int getDateByField(int field) {
        Calendar now = Calendar.getInstance();
        return now.get(field);
    }

    /**
     * field获取不同时间
     *
     * @param field
     * @return
     */
    public static int getDateByField(Date date, int field) {
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        return now.get(field);
    }

    /**
     * 计算两个时间相差多少个年
     *
     * @return
     * @throws ParseException
     */
    public static int yearsBetween(String start, String end) throws ParseException {
        Calendar startDate = Calendar.getInstance();
        Calendar endDate = Calendar.getInstance();
        startDate.setTime(stringDate(start, "yyyy-MM-dd"));
        endDate.setTime(stringDate(end, "yyyy-MM-dd"));
        return (endDate.get(Calendar.YEAR) - startDate.get(Calendar.YEAR));
    }

    /**
     * 计算两个时间相差多少个月
     *
     * @return int
     * @throws ParseException
     */
    public static int monthsBetween(String start, String end) throws ParseException {
        Calendar startDate = Calendar.getInstance();
        Calendar endDate = Calendar.getInstance();
        startDate.setTime(stringDate(start, "yyyy-MM-dd"));
        endDate.setTime(stringDate(end, "yyyy-MM-dd"));
        return yearsBetween(start, end) * 12 + endDate.get(Calendar.MONTH) - startDate.get(Calendar.MONTH);
    }


    /**
     * 获取指定日期的某天的日期
     *
     * @param date
     * @param day
     * @return
     */
    public static Date getDateDay(Date date, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, day);
        return calendar.getTime();
    }

    /**
     * 获取指定日期的某天的日期
     *
     * @param date
     * @param day
     * @return
     */
    public static String getDateDay(Date date, int day, String pattern) {
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        return df.format(getDateDay(date, day));
    }
}